//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 5201-mac on 2017/4/16.
//  Copyright © 2017年 wlb-mac. All rights reserved.
//

//在一个ScrollView上添加四张图片和pageControl

import UIKit

private let imageCount = 4

class NewFeatureViewController: UIViewController, UIScrollViewDelegate {
    
    //懒加载视图（scrollView）
    lazy var scrollView: UIScrollView = {
        let sv = UIScrollView(frame: UIScreen.main.bounds)
        //设置代理
        sv.delegate = self
        sv.backgroundColor = UIColor.orange
        return sv
    }()
    
    lazy var pageControl: UIPageControl = {
        let page = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 80, width: ScreenWidth, height: 30))
        //设置显示的总数
        page.numberOfPages = imageCount
        //设置普通状态下的颜色
        page.pageIndicatorTintColor = UIColor.gray
        //设置选中的颜色
        page.currentPageIndicatorTintColor = UIColor.blue
        return page
    }()

    override func loadView() {
        //view = scrollView
        super.loadView()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setScrollView()
        setPageControl()
    }
    
    //封装设置引导页图片的方法
    func setScrollView() {
        self.view.addSubview(scrollView)
        //添加四张图片
        let width = scrollView.bounds.width
        let height = scrollView.bounds.height
        
        for i in 0..<imageCount {
            let imageName = String(format: "new_feature_%d", i + 1)
            let image = UIImage(named: imageName)
            //获取图片视图
            let iv = UIImageView(image: image)
            iv.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: height)
            //添加到scrollView
            scrollView.addSubview(iv)
            
            if i == imageCount - 1 {
                //最后一页，添加按钮
                let startBtn = UIButton()
                startBtn.setTitle("马上开始", for: .normal)
                startBtn.backgroundColor = UIColor.orange
                startBtn.center = CGPoint(x: self.view.center.x, y: self.view.center.y + 120)
                startBtn.bounds.size = CGSize(width: 120, height: 45)
                startBtn.addTarget(self, action: #selector(NewFeatureViewController.startBtnDidClick), for: .touchUpInside)
                iv.addSubview(startBtn)
                iv.isUserInteractionEnabled = true
            }
        }
        
        //设置contentSize
        scrollView.contentSize = CGSize(width: CGFloat(imageCount) * width, height: 0)
        
        //基本设置
        //设置弹簧效果
        scrollView.bounces = false
        //滚条隐藏
        scrollView.showsHorizontalScrollIndicator = false
        //分页滚动
        scrollView.isPagingEnabled = true
        
    }
    
    func startBtnDidClick() {
        UIView.animate(withDuration: 1.5, animations: {
            self.view.alpha = 0.2
            self.view.transform = CGAffineTransform(scaleX: 4, y: 4)
        }) { (_) in
            self.scrollView.removeFromSuperview()
            self.pageControl.removeFromSuperview()
            
            //切换根视图控制器，显示TabbarController
            //TODO:根视图控制器的切换
            UIApplication.shared.keyWindow?.rootViewController = MainViewController()
        }
    }
    
    func setPageControl() {
        self.view.addSubview(pageControl)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取水平方向的位移
        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5
        let page = Int(offsetX / scrollView.bounds.width)
        //设置pageControl显示的页数
        pageControl.currentPage = page
        
    }
    

}
